package org.xurui.sxl.protocol.command.impl;

import org.jetlinks.core.message.DeviceMessage;
import org.jetlinks.core.message.codec.FromDeviceMessageContext;
import org.jetlinks.core.message.property.ReportPropertyMessage;
import org.xurui.sxl.protocol.command.CmdCodec;
import org.xurui.sxl.protocol.enums.CommandEnum;
import org.xurui.sxl.protocol.utils.BinaryUtils;
import org.xurui.sxl.protocol.utils.NumberUtils;
import org.xurui.sxl.protocol.utils.SxlMessageUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 短信息状态同步
 */
public class CmdCodec_SMSSTATUS implements CmdCodec {
    @Override
    public boolean supportsCmd(String cmd) {
        return CommandEnum.SMSSTATUS.name().equals(cmd);
    }

    @Override
    public boolean supportsRelayCmd() {
        return false;
    }

    @Override
    public List<DeviceMessage> parsePayload(String payload, FromDeviceMessageContext context) {
        //短信息状态同步 设备下发 TODO 没有 SV,BAT
        //格式：*XX,YYYYYYYYYYYYYYY,SMSSTATUS,HHMMSS,SMSUID,STATUS,S,latitude,D,longitude,G,speed,pdop,DDMMYY,track_status#
        //例子：*LX,2020916301,SMSSTATUS,173030,6F9619FF-8B86-D011-B42D-00C04FC964FF,0,A,2230.7021,N,11355.1445,E,0.0,2.8,271112,FFFFFFFF#
        //回复格式：*XX,YYYYYYYYYYYYYYY,V4,SMSSTATUS,seq,HHMMSS#
        //回复示例：*LX,2020916301,V4,SMSSTATUS,173030,173031#
        //参数说明：SMSUID 短消息 ID；GUID,对应短消息下发的 ID
        //参数说明：STATUS 状态：0-删除；1:-已读；2-其他
        //解析数据为属性上报
        ReportPropertyMessage report = ReportPropertyMessage.create();
        String[] payloadArr = payload.replaceAll("\\*", "").split(",");
        String deviceId = payloadArr[1];
        report.setDeviceId(deviceId);
        report.setCode(CommandEnum.SMSSTATUS.name());
        report.setMessageId(deviceId + "_" + payloadArr[3]);
        report.setTimestamp(System.currentTimeMillis());
        Map<String, Object> properties = new HashMap<>();
        properties.put("uptime",payloadArr[3]);
        properties.put("smsUID", payloadArr[4]);
        properties.put("status", NumberUtils.parseInt(payloadArr[5]));
        properties.put("s",payloadArr[6]);
        properties.put("latitude",BinaryUtils.getWgs84LonLat(payloadArr[7],"lat"));
        properties.put("d",payloadArr[8]);
        properties.put("longitude",BinaryUtils.getWgs84LonLat(payloadArr[9],"lon"));
        properties.put("g",payloadArr[10]);
        properties.put("speed",NumberUtils.parseDouble(payloadArr[11]));
        properties.put("pdop",NumberUtils.parseDouble(payloadArr[12]));
        properties.put("upday",payloadArr[13]);
        String trackerStatusHex = payloadArr[14];

        Map<String,Object> trackerStatusMap = parseTrackerStatus(trackerStatusHex);
        properties.put("trackerstatus",trackerStatusMap);
        report.setProperties(properties);
        //回复设备数据
        //TODO 暂时没找到设备回复消息的方法
       /* String day= BinaryUtils.getDateFormat();
        String reply="*"+payloadArr[0]+","+deviceId+",V4,"+CommandEnum.SMSSTATUS.name()+","+payloadArr[3]+","+day;
        context.getSession()
            .send(EncodedMessage
                .simple(Unpooled.wrappedBuffer(reply.getBytes())))
            .subscribe();*/
        SxlMessageUtils.bulidAndSend2DeviceV4Msg(context,payloadArr[0],deviceId,CommandEnum.SMSSTATUS.name(),payloadArr[3]);
        return genDeviceMessage(report);
    }
}
